<?php

namespace catchAdmin\stock\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\stock\model\Locking as LockingModel;
use think\Exception;
use think\Response;
use think\facade\Db;
use catcher\CatchAuth;

class Locking extends CatchController
{
    
    protected $lockingModel;
    
    /**
     *
     * @time 2022/06/07 16:46
     * @param LockingModel $lockingModel
     * @return mixed
     */
    public function __construct(LockingModel $lockingModel)
    {
        $this->lockingModel = $lockingModel;
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth,Request $request)
    {
        $user = $auth->user();
        $key = $request->post('key');
        return CatchResponse::paginate($this->lockingModel->getList2($user['tenant_id'],$key,$user['id']));
    }

    /**
     *
     * @time 2022/06/07 16:46
     * @param Request $request
     * @return Response
     */
    public function save(Request $request) : Response
    {
        return CatchResponse::success($this->lockingModel->storeBy($request->post()));
    }

    /**
     *
     * @time 2022/06/07 16:46
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update($id, Request $request) : Response
    {
        return CatchResponse::success($this->lockingModel->updateBy($id, $request->post()));
    }

    /**
     *
     * @time 2022/06/07 16:46
     * @param $id
     * @return Response
     */
    public function delete($id)
    {
        $pick= Db::table('db_pick')->field('number,warehouse_out_son_id')->where('id',$id)->find();

        Db::startTrans();
        try {
            //减少数量
            Db::table('db_warehouse_out_son')->where('warehouse_out_son_id',$pick['warehouse_out_son_id'])->dec('pick_number',floor($pick['number']))->update();
            //删除锁定管理
            $res = $this->lockingModel->deleteBy($id);
            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new Exception('锁定管理删除失败' . $exception->getMessage());
        }

        return CatchResponse::success($res);
    }
    public function delAll(Request $request)
    {
        $ids = $request->param('id');
        $where = [];
        $where[] = ['id','in',$ids];
        $list = Db::table('db_pick')->where($where)->select()->toArray();
        Db::startTrans();
        foreach ($list as $k=>$v){
            $res1 = Db::table('db_pick')->where('id',$v['id'])->update(['deleted_at'=>time()]);
            $res2 = Db::table('db_warehouse_out_son')
                ->where('warehouse_out_son_id',$v['warehouse_out_son_id'])
                ->dec('pick_number',floor($v['number']))
                ->update();
            if(!$res1||!$res2){
                Db::rollback();
                return CatchResponse::fail([$res1,$res2]);
            }
        }
        Db::commit();
        return CatchResponse::success('操作成功！');
    }
}